<?php 
include('../Connections/KRAPPERCON.php');
include ("../Connections/Accesos.php");

$sql = "call sp_getNextNumCentroPhp('GU_FOLIO','".$usr_ctr."')";
$res = $mysqli->query($sql);
$movim = getVal('doc');

$next = '';

if ($res){
	if ($row = $res->fetch_assoc()){	
	$next = $row['w_guia'];
	}
$res->close();
$mysqli->check_result();
}

?>
<table width="100%" border="0" id="tbl_frm_gfa" cellspacing="0" cellpadding="0">
  <tr>
    <td>Documento : </td>
    <td>
      <input type="text" name="t_gu_prev" id="t_gu_prev" value="<?php echo getVal('doc');?>" disabled>
    </td>
    <td>Asignacion :</td>
    <td><input type="text" name="t_gu_ref" id="t_gu_ref" disabled="disabled" style="font-weight:bold" value="<?php echo $next;?>" /></td>
  </tr>
  <tr>
    <td>RUC/DNI Transportista:</td>
    <td colspan="3">
      <input name="t_gu_nif" type="text" class="clsReq" id="t_gu_nif" size="12" maxlength="20" onKeyPress="introNIF(event,1)">&nbsp;
      <input type="submit" name="btn_busDes2" id="btn_busDes2" value="Buscar" onclick="searchCliGui(1)" /></td>
  </tr>
  <tr>
    <td colspan="4">
    <div id="d_nif_gu" style="max-height:100px;overflow:auto"></div>    </td>
  </tr>
  <tr>
    <td><strong>Codigo Transportista:</strong></td>
    <td colspan="3">
      <input name="t_gu_cdc" value="[Auto]" type="text" id="t_gu_cdc" size="12" maxlength="12" disabled>&nbsp;&nbsp;<input type="button" value="Nuevo" onClick="clcNewTra()">    </td>
  </tr>
  <tr>
    <td>Nombre/Razon Social :</td>
    <td colspan="3">
      <input type="text" class="dg noView" id="t_gu_nif" size="15" maxlength="15" disabled="disabled" />
      <input class="dg" name="t_gu_raz" type="text" id="t_gu_raz" size="50" maxlength="60" disabled></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="3"><input class="dg" name="t_gu_dir" type="text" id="t_gu_dir" size="60" maxlength="60" disabled="disabled" /></td>
  </tr>
  <tr>
    <td colspan="4">&nbsp;</td>
  </tr>
  <tr>
    <td><strong>Punto de Expedicion:</strong></td>
    <td colspan="3">
    <select name="cbo_pto_exp" class="clsReq" id="cbo_pto_exp" onchange="selExp(this)">
    <option selected="selected" value="">Seleccione...</option>
	<?php 
	include ('../Query_Min/getCentros.php');
	?></select>&nbsp;&nbsp;<span id="sp_dirExp"></span>
    <!-- 
      <input class="dg" name="t_gu_dir" type="text" id="t_gu_dir" size="60" maxlength="60" disabled>
    --></td>
  </tr>
  
  <?php 
 /* if (getVal('factura')=='X')
  {
  }
	else {
	*/
  ?>
  <tr class="destino">
    <td colspan="4">&nbsp;</td>
  </tr>
  <tr class="destino">
    <td><strong>Cliente Destino:</strong></td>
    <td colspan="3"><input name="t_gu_cdc2" class="clsReq" type="text" id="t_gu_cdc2" size="12" maxlength="10" onkeypress="if(event.keyCode==13) showCliGui2({cod:this.value});" />
      <input type="button" name="btn_busDes" id="btn_busDes" value="Buscar" onclick="searchCliGui(2)" /></td>
  </tr>
  <tr class="destino">
    <td>Nombre/Razon Social :</td>
    <td colspan="3"><input type="text" class="dg" disabled="disabled" id="t_gu_nif2" size="15" maxlength="15" />
      &nbsp;<input class="dg" name="t_gu_raz2" type="text" id="t_gu_raz2" size="50" maxlength="60" disabled="disabled" /></td>
  </tr>
  <tr class="destino">
    <td>Direccion:</td>
    <td colspan="3"><input class="dg" name="t_gu_dir2" type="text" id="t_gu_dir2" size="60" maxlength="60" disabled="disabled" /></td>
  </tr>
  <?php 
  //}
  ?>
  <tr>
    <td>&nbsp;</td>
    <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
    <td>Texto Guia :</td>
    <td colspan="3">
      <textarea name="t_gu_txt" cols="60" rows="3" id="t_gu_txt"></textarea>
    </td>
  </tr>
  <tr>
    <td>
      <input type="button" name="b_doGuia" id="b_doGuia" onClick="returnDoGuia();" value="Generar">
    </td>

    <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="3">&nbsp;</td>
  </tr>
</table>
<script type="text/javascript">

<?php 
if (getVal('factura') == 'X'){
$partida = getVal('partida');
//$destino = getVal('destino');
?>
GEBI('cbo_pto_exp').value = '<?php echo $partida;?>';
GEBI('sp_dirExp').innerHTML = $("#cbo_pto_exp").find(':selected').attr('title');
<?php 
// Si es factura, el cliente destino se carga automatico
$cliente = getVal('cliente');
$sql = "select ClientID,ClientNom,ClientDirec,ClientNIF from tt_cliente where clientID = '".$cliente."'";
//echo $sql;
$res = $mysqli->query($sql) or die($mysqli->error.__LINE__); ;
	if ($res){
		if ($row=$res->fetch_assoc()){
		?>
			GEBI('t_gu_cdc2').value = '<?php echo $row['ClientID'];?>';
			GEBI('t_gu_raz2').value = '<?php echo $row['ClientNom'];?>';
			GEBI('t_gu_dir2').value = '<?php echo $row['ClientDirec'];?>';
			GEBI('t_gu_nif2').value = '<?php echo $row['ClientNIF'];?>';		
		<?php 
		}
	$res->close();
	$mysqli->check_result();
	}
}
else {
$partida = getVal('partida');
$destino = getVal('destino');
?>
if (pInt('<?php echo $partida;?>')>0 ) {
GEBI('cbo_pto_exp').value = '<?php echo $partida;?>';
GEBI('sp_dirExp').innerHTML = $("#cbo_pto_exp").disable().find(':selected').attr('title');
}

<?php 
	$sql = //"select '',ClientID,ClientNom,ClientDirec,ClientNIF from tt_cliente where clientID = '".$partida."' union all ".
	"select ClientID,ClientNom,ClientDirec,ClientNIF from tt_cliente where clientID = '".$destino."'";
//echo $sql;
$res = $mysqli->query($sql) or die($mysqli->error.__LINE__); ;
	if ($res){
	if($row=$res->fetch_assoc()){
		?>
			GEBI('t_gu_cdc2').value = '<?php echo $row['ClientID'];?>';
			GEBI('t_gu_raz2').value = '<?php echo $row['ClientNom'];?>';
			GEBI('t_gu_dir2').value = '<?php echo $row['ClientDirec'];?>';
			GEBI('t_gu_nif2').value = '<?php echo $row['ClientNIF'];?>';		
		<?php 
		}
	$res->close();
	$mysqli->check_result();
	}
}
?>

function showCliGui1(cli){
	var dato = getClienteGuia('',cli);		
}

function showCliGui2(cli){
	var dato = getClienteGuia('2',cli);		
}


function getClienteGuia(a,obj){
	var codcli = obj.cod;
	//$(obj).attr('title','');
	//var chk = GEBI('chk_pd_cli').checked;
	$('#t_gu_cdc'+a).txtLoad();
	var nif = '';
	sendData('Query_Min/getDatCliente.php',{ t_codcli : codcli , nif : '' , field : 'venta' }, function(){
		var dato = eval ('(' + $('#d_result').html() + ')');
//		alert(dato.nom);
		$('#t_gu_cdc'+a).etxtLoad();
		if ($.trim(dato.nom) != null && $.trim(dato.nom) != ''){
//		return dato;
		
		GEBI('t_gu_cdc'+a).value = codcli;
		GEBI('t_gu_raz'+a).value = dato.nom;
		GEBI('t_gu_dir'+a).value = dato.dir;
		GEBI('t_gu_nif'+a).value = dato.nif;		
		//closexPopup();
		//alert(dato.nom);
		}
		else {		
		alert('El Cliente ingresado no existe') ;
		dato =  {nom : '',cod : '', dir : '', ruc : '', raz : '',nif : ''};
		GEBI('t_gu_raz'+a).value = dato.nom;
		GEBI('t_gu_dir'+a).value = dato.dir;
		GEBI('t_gu_nif'+a).value = dato.nif;	
		return dato;
			//alert('as');
			//$('#sp_nomcli').html('');
			//if ( confirm('El Cliente ingresado no existe, desea crealo?') ){
				//loadPopup({ url : 'Mantenimientos/MantCli.php' , params : { return : 'showCliPed', prevCli : codcli } });
//			}
		}
	});
}



function searchCliGui(a){
loadPopup({url:'Consultas/findCliente.php', params : { return : 'showCliGui'+a , noClose : 'X' } });
}

function selExp(obj){
var dir = '';
if (obj.selectedIndex == 0){
dir = '';
}
else {
dir= $(obj).find(':selected').attr('title');
}
GEBI('sp_dirExp').innerHTML = dir;
}

function returnDoGuia(){
	//alert("<?php echo getVal('return');?>");
	if (!$("#tbl_frm_gfa").checkInput()){
	 return;
	}
	<?php echo getVal('return'); ?>
}

function doGuiaMov(doc){
	//var doc = GEBI('t_gu_prev').value;
	sendData('Procesos/Opers/OperMigo.php', 
		{ 
		oper : 'guia' ,
		movimiento : doc,
		ClientID : GEBI('t_gu_cdc').value,
		ClientNIF : GEBI('t_gu_nif').value,
		ClientNom : GEBI('t_gu_raz').value,
		ClientDir : GEBI('t_gu_dir').value,
		Destino : GEBI('t_gu_cdc2').value,
		Partida : GEBI('cbo_pto_exp').value,
		texto : GEBI('t_gu_txt').value
		} 
	);
}

</script>

